﻿using System.ComponentModel.DataAnnotations;

namespace Infrastructure.Utils
{
    #region 必填项
    public class CustomRequiredAttribute : ValidationAttribute
    {
        //用法：[Required(ErrorMessage = "xxx")]
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (string.IsNullOrWhiteSpace(Convert.ToString(value))) return new ValidationResult(ErrorMessage ?? $"{validationContext.DisplayName}是必需项！");
            return ValidationResult.Success;
        }
    }
    #endregion

    #region 手机号
    public class CustomMobileAttribute : ValidationAttribute
    {
        //private bool _isTrim { get; }
        //private string _errorMessage { get; }
        //public CustomMobileAttribute(bool p, string errorMessage)
        //{
        //    _isTrim = p;
        //    _errorMessage = errorMessage;
        //}
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //if(_isTrim) 
            if (!RegexPattern.IsMobile(Convert.ToString(value))) return new ValidationResult(ErrorMessage ?? $"手机号码输入错误！");
            return ValidationResult.Success;
        }
    }
    #endregion

    #region 身份证号
    public class CustomIdCardAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!RegexPattern.IsIdCard(Convert.ToString(value))) return new ValidationResult(ErrorMessage ?? $"身份证号输入错误！");
            return ValidationResult.Success;
        }
    }
    #endregion

    #region 自然数
    public class CustomNaturalNumberAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!RegexPattern.IsNaturalNumber(Convert.ToString(value))) return new ValidationResult(ErrorMessage ?? $"请输入自然数！");
            return ValidationResult.Success;
        }
    }
    #endregion

    #region 非负数
    public class CustomNonnegativeNumberAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!RegexPattern.IsNonnegativeNumber(Convert.ToString(value))) return new ValidationResult(ErrorMessage ?? $"请输入正确的数值！");
            return ValidationResult.Success;
        }
    }
    #endregion

    #region 正则验证
    /// <summary>
    /// 正则验证
    /// </summary>
    public class RegexAttribute : ValidationAttribute
    {
        public RegexAttribute(string p, string errorMessage)
        {
            _pattern = p;
            _errorMessage = errorMessage;
        }
        private string _pattern { get; }
        private string _errorMessage { get; }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!RegexPattern.IsMatch(Convert.ToString(value), _pattern)) return new ValidationResult(_errorMessage ?? "请输入正确的内容！");
            return ValidationResult.Success;
        }
    }
    #endregion
}
